package com.heima.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.heima.dto.FieldDto;
import com.heima.dto.InsertDictionaryDto;
import com.heima.dto.pagedto.PageDictionaryDto;
import com.heima.pojo.DictionaryType;
import com.heima.mapper.DictionaryMapper;
import com.heima.pojo.Dictionary;
import com.heima.service.DictionaryService;
import com.heima.vo.FieldListVo;
import com.heima.vo.FieldOneVo;
import com.heima.vo.FieldTypeOneVo;
import com.heima.vo.FieldVo;
import com.heima.vo.PageResult;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.List;

@Service
@Slf4j
@RequiredArgsConstructor
public class DictionaryServiceImpl implements DictionaryService {

    private final DictionaryMapper dictionaryMapper;

    private final HttpServletResponse httpServletResponse;

    /**
     * 从字典中查询字段列表
     * @param dto
     * @return
     */
    @Override
    public PageResult queryByDictionary(PageDictionaryDto dto) {
        PageHelper.startPage(dto.getPageNum(),dto.getPageSize());
        Page<DictionaryType>  page = dictionaryMapper.queryByDictionary(dto);
        PageResult vo = new PageResult();
        vo.setTotal(page.getTotal());
        vo.setRows(page.getResult());
        return vo;
    }

    /**
     * 系统功能开关
     * @param filedName
     * @return
     */
    @Override
    public FieldListVo queryField(String filedName) {
        FieldListVo vo = new FieldListVo();
        vo.setData(dictionaryMapper.queryField(filedName));
        return vo;
    }

    /**
     * 添加字典字段
     * @param dto
     */
    @Override
    public void insertDictionaryType(InsertDictionaryDto dto) {
        dto.setCreateTime(LocalDateTime.now());
        dto.setCreateBy("admin2");
        System.out.println(dto);
        dictionaryMapper.insertDictionaryType(dto);
    }

    /**
     * 查需字典具体内容
     * @param id
     * @return
     */
    @Override
    public FieldTypeOneVo queryByDictionaryFiled(Integer id) {
        DictionaryType type =  dictionaryMapper.queryByDictionaryFiled(id);
        FieldTypeOneVo vo = new FieldTypeOneVo();
        vo.setData(type);
        return vo;
    }

    /**
     * 根据类型查询字典字段
     * @param dto
     * @return
     */
    @Override
    public PageResult queryByDictionaryType(PageDictionaryDto dto) {
        PageHelper.startPage(dto.getPageNum(),dto.getPageSize());
        Page<Dictionary>  page = dictionaryMapper.queryByDictionaryType(dto);
        PageResult vo = new PageResult();
        vo.setRows(page.getResult());
        return vo;
    }

    /**
     * 添加字典字段内容
     * @param dto
     */
    @Override
    public void insertDictionaryField(FieldDto dto) {
        dto.setCreateTime(LocalDateTime.now());
        dictionaryMapper.insertDictionaryField(dto);
    }

    /**
     * 根据id查询字典字段内容
     * @param id
     * @return
     */
    @Override
    public FieldOneVo findFieldById(Integer id) {
        Dictionary dictionary = dictionaryMapper.findFieldById(id);
        FieldOneVo vo = new FieldOneVo();
        vo.setData(dictionary);
        return vo;
    }

    /**
     * 修改字典类型
     * @param dto
     */
    @Override
    public void updateDictionaryType(InsertDictionaryDto dto) {
        dto.setUpdateTime(LocalDateTime.now());
        dictionaryMapper.updateDictionaryType(dto);
    }

    /**
     * 根据id删除字典类型
     * @param ids
     */
    @Override
    public void deleteDictionaryType(List<Integer> ids) {
        dictionaryMapper.deleteDictionaryType(ids);
    }

    /**
     * 根据id修改字典字段内容
     * @param dto
     */
    @Override
    public void updateField(FieldDto dto) {
        dictionaryMapper.updateField(dto);
    }

    /**
     * 根据id删除字典字段内容
     * @param ids
     */
    @Override
    public void deleteDictionaryField(List<Integer> ids) {
        dictionaryMapper.deleteDictionaryField(ids);
    }

    /**
     * 商机池捞取用户
     * @param ids
     */
    @Override
    public void scooping(List<Integer> ids) {

    }

}
